package com.hioove.core.process;

import com.hioove.core.container.IContainer;
import com.hioove.core.filter.FilterChain;
import com.hioove.core.ioc.model.AttributeInjection;
import com.hioove.core.model.base.config.*;
import com.hioove.core.util.Builder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

public class Start {
    static Logger log = LoggerFactory.getLogger(Start.class);

    public static Object app(Class<?> app, String... args) {
        // 1. 加载启动类 分析启动类注解
        StartClassInfo startClassInfo = Load.startClass(app, args);
        // 2. 加载配置信息
        ConfigInfor configInfo = Load.configInfo(startClassInfo, args);
        // 3. 扫描待实例化的对象 将要实例化
        Map<String, ClassSupport> tobeInstance = Load.scanObjectsToBeInstantiated(startClassInfo, configInfo);
        // 4. 加载配置类(主要  涉及带有配置类注解的类实例化 。eg. Component.class Bean.class (只涉及Bean的实例化)
        ConfigInstance configInstance = Load.configObj(startClassInfo, configInfo, tobeInstance);
        // 5. 初步实例化类对象 完成类增强 eg. RestController.class  Service.class  Repository.class (只涉及Bean的实例化 包括增强类)
        Map<Class<?>, Object> mvcObj = Load.mvcObj(startClassInfo, configInfo, tobeInstance, configInstance);
        // 6. 注入实例见对象 完成最终实例化
        AttributeInjection attributeInjection = Load.resolveInterdependence(startClassInfo, configInfo, tobeInstance, configInstance, mvcObj);
        // 7. 初始化调度器  filter dispatch
        HashMap<String, MethodSupport> dispatchInfo = Load.initDispatch(tobeInstance, attributeInjection);
        // 8. 初始化过滤器和  filter
        FilterChain filterInfo = Load.initFilterAnd(configInstance);
        // 9. 启动容器
        Builder.of(IContainer.INSTANCE)
                .with(IContainer::setStartupParam, startClassInfo)
                .with(IContainer::setConfigInfo, configInfo)
                .with(IContainer::setDispatch, dispatchInfo)
                .with(IContainer::setFilter, filterInfo)
                .build().start();
        return new Object();
    }
}

